﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TomNet.IMSC.Core.Contracts.Common;
using TomNet.IMSC.Core.Contracts.Organization;
using TomNet.IMSC.Model.DbModels.Organization;
using TomNet.IMSC.Web.Common;
using TomNet.IMSC.Web.Filters;
using TomNet.IMSC.Web.Models.ApiParams;
using TomNet.Web.Mvc.UI;
using TomNet.Web.WebApi;

namespace TomNet.IMSC.Web.Controllers.API
{
    [Description("API-广告")]
    [ApiAuthentication]
    public class AdvertisementInterfaceController : BaseApiController
    {
        public IAdvertisementContract AdvertisementContract { get; set; }
        public IImagesContract ImagesContract { get; set; }
        public IUsersContract UsersContract { get; set; }
        public IAgentCityContract AgentCityContract { get; set; }
        public IHttpActionResult ttGetAdvertisementInterfaceList([FromBody]AdvertisementModel entity)
        {
            int userid = BaseWebApiExt.GetUserIDByToken();
            Users u = UsersContract.GetByKey(userid);
            int agentid = AgentCityContract.Entities.Where(p => p.CityID == u.CityID).FirstOrDefault().AgentID;
            var query = (from a in AdvertisementContract.TrackEntities.Where(m => !m.IsDeleted && m.ModuleID == entity.ModuleID && m.AgentID == agentid)
                         select new AdvertisementListModel
                         {
                             Id = a.Id,
                             DynamicID = a.DynamicID,
                             AdvertisementType = a.AdvertisementType,
                             Text = a.Text,
                             ImagePath = (from img in ImagesContract.Entities
                                          where img.GroupID == 11 && img.ImageType == 1 && a.Id == img.InterfixID && img.IsDeleted == false
                                          select img.ImagePath + img.FileName).ToList()
                         }).ToList();
            var result = new AjaxResult("成功", AjaxResultType.Success, query);
            return Token(result, "");
        }
    }
}